<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<table id="plan_task_table" class="table table-bordered">
</table>
<script type="text/javascript">
    $(function () {
        var plan_task_table = $('#plan_task_table').datagrid({
            height: '100%',
            width:'100%',
            fullGrid:true,
            columnMenu: false,
            contextMenuH:false,
            fieldSortable:false,
            gridTitle: '系统路径',
            showToolbar: true,
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-blue" onclick="savePlanTask();" data-icon="plus"><i class="fa fa-plus"></i> 添加</button>';
            },
            toolbarItem: "refresh,|,del",
            filterThead: false,
            delPK: 'id',
            dataUrl: '/quartz/list',
            local: "remote",
            inlineEditMult: false,
            saveAll: false,
            addLocation: 'last',
            editUrl: '/admin/plan/task/save',
            delUrl: '/quartz/deleteById',
            delConfirm: true,
            delCallback: function (response) {
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                }
            },
            columns: [
                {
                    name: 'name',
                    label: '配置名称',
                    align: 'center',
                    rule: 'required',
                    add: false
                }, {
                    render: jobSwitchFun,
                    align: 'center',
                    label: '开/关'
                }, {
                    name: 'remark',
                    label: '说明',
                    align: 'center'
                }, {
                    render: operation,
                    label: '操作列'
                }
            ],
            paging: {pageSize: 20, pageCurrent: 1},
            linenumberAll: true
        });

    });

    function operation(value, data) {
        var html = '<button type="button" class="btn-green" onclick="savePlanTask(\'' + data.id + '\');">编辑</button>'
                + '<button type="button" class="btn-red" data-toggle="del.datagrid.tr">删除</button>';
        return html
    }

    function jobSwitchFun(value, data) {
        if (data.enabled) {//开状态
            return '<div data-val="false" data-switchid = "' + data.id + '" class="selectBtn" onclick="saveJobSwitch(this);"><div class="openWord">开</div><div class="open1 select_01"><div class="open2 select_02"></div></div><div class="closeWord">关</div></div>';
        } else {
            return '<div data-val="true" data-switchid = "' + data.id + '" class="selectBtn" onclick="saveJobSwitch(this);"><div class="openWord">开</div><div class="open1 select_01 close1"><div class="select_02"></div></div><div class="closeWord">关</div></div>';
        }
    }

   function saveJobSwitch(e){
        var $this = $(e);
        var id = $this.data("switchid");
        var status = $this.data("val");
        if (id == null || id == "" || typeof (id) == "undefined" || status == null || status === "" || typeof (status) == "undefined") {
            BJUI.alertmsg('error', "参数错误，重新操作", {
                okCall: function () {
                    BJUI.navtab('refresh')//刷新当前窗口
                }
            });
            return;
        }

        if ($('.select_01').attr("class") != "close1") {
            $this.find('.select_01').toggleClass('close1');
            $this.find('.select_01').children('.select_02').toggleClass('open2');
        }

        BJUI.ajax('doajax', {
            url: '/quartz/updateStatus',
            loadingmask: true,
            data: {"id": id, "enabled": status},
            callback: function (response, options) {
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            $('#plan_task_table').datagrid("refresh",true);
                        }
                    });

                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            $('#plan_task_table').datagrid("refresh",true);
                        }
                    });
                }
            }
        });

    };

    function savePlanTask(id) {
        var url = "/quartz/savePage";
        if (id != "" && id != null && typeof (id) != "undefined") {
            url = url + "?id=" + id;
        }
        BJUI.dialog({
            id: 'save_quartz_job',
            url: url,
            title: '编辑',
            resizable: true,
            width: 900,
            height: 730,
            fresh: true
        });
    }
</script>